package com.xxwu.trans.controller;

import com.xxwu.trans.model.UserInfo;
import com.xxwu.trans.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class TransactionController {
    @Autowired
    private UserInfoService userInfoService;

    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r1")
    public String r1(UserInfo userInfo){
        Integer result = userInfoService.insert(userInfo);
        int a = 10/0;
        return "添加成功" + result;
    }
    @Transactional
    @RequestMapping("/r2")
    public String r2(UserInfo userInfo){
        Integer result = userInfoService.insert(userInfo);
        try{
            int a = 10/0;
        }catch (Exception e){
            log.info("发生异常");
            throw e;
        }
        return "添加成功" + result;
    }
}
